package com.zs4sdk.activity.resetPassword;

import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import com.common.library.data.CommonEventHandler;
import com.common.library.data.CommonHandle;
import com.common.library.data.EventHandler2Get;
import com.common.library.entity.JSONResultEntity;
import com.common.library.util.ActivityHelper;
import com.common.library.util.EncryptHelper;
import com.common.library.util.HandlerUtil;
import com.common.library.util.Helper;
import com.common.library.util.ParamsHelper;
import com.zs4sdk.activity.Zs4GameBaseActivity;
import com.zs4sdk.common.Extra;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/6/12.
 */

public class Zs4GameResetPasswordFt extends Zs4GameResetPassword {
    protected EditText mOldPasswordView;
    Zs4GameResetPasswordFt(Zs4GameBaseActivity act, View view) {
        super(act, view);
    }
    //获得参数
    public void getParams() {

    }
    public void init(){
        super.init();
        View view = findView(ActivityHelper.getIdResId("zs4gameviewid_act_reset_pwd_input_old_pwd"));
        view.setVisibility(View.VISIBLE);
        View tvZs4gameOldPassword =  findView(ActivityHelper.getIdResId("tvZs4gameOldPassword"));
        tvZs4gameOldPassword.setVisibility(View.VISIBLE);
        mOldPasswordView = (EditText) findView(ActivityHelper.getIdResId("zs4gameviewidewPasswordView_act_old_password"));

    }
    protected void resetPassword(){
        String newPassword = mNewPasswordView.getText().toString().trim();
        String oldPassword = mOldPasswordView.getText().toString();
        httpGetForgetCodeNewPassword(oldPassword, newPassword);
    }

    //点击提交后的处理
    public void cilcksubmit() {
        String oldPassword = mOldPasswordView.getText().toString();
        if(oldPassword.length() == 0){
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gameOldPasswordNoNull"));
        }else if(oldPassword.length() < 6 || oldPassword.length() > 12){
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_length"));
        }else if (Helper.checkChinese(oldPassword)) {
            // 密码为非中文
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_unchinese"));
        }else{
            if(isPasswordError()){

            } else{
                // httpGetForgetCodeNewPassword(mobile, newPassword);

                resetPassword();
            }
        }
    }

    /**
     * 请求重置密码
     *
     * @param oldPassword
     * @param newPassword
     */
    private void httpGetForgetCodeNewPassword(final String oldPassword, final String newPassword) {

        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.SHOWPROCESS);

        ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
        //paramList.add(new BasicNameValuePair("old_password", EncryptHelper.encryptByAES(mobile)));
        paramList.add(new BasicNameValuePair("old_password", EncryptHelper.encryptByAES(EncryptHelper.md5(oldPassword))));
        paramList.add(new BasicNameValuePair("new_password", EncryptHelper.encryptByAES(EncryptHelper.md5(newPassword))));
//
        new EventHandler2Get(
                Extra.URL.RESET_ACCOUNT_PASSWORD,
                paramList,
                ParamsHelper.getUserAgent1(),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {

                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);

                        Message msg = myHandle.obtainMessage();

                        msg.what = Extra.MessageKey.REQUEST_SUCCESS;
                        msg.obj = result;

                        // 发送消息
                        myHandle.sendMessage(msg);

                    }

                    @Override
                    public void doExceptionHandler() {
                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);
                    }
                });
    }


    CommonHandle myHandle = new CommonHandle(mActivity, new CommonHandle.HandleMessage() {

        @Override
        public void doHandleMessage(Message msg) {

            if (Extra.MessageKey.REQUEST_SUCCESS == msg.what) {
                JSONResultEntity entity = new JSONResultEntity((JSONObject) msg.obj);
                if (entity.getResult() == Extra.RESULTCODE.SUCCESS) {
                    ActivityHelper.showToast(ActivityHelper.getStringResId("zs4gamestring_hint_please_login_afterresetpassword"));
                    mActivity.finish();

                } else {
                    //请求失败提示
                    sunmitTipTextView.setText(entity.getDesc());
                }

            }

        }
    });
}
